import os

from framework.httpRequest import HttpRequest
from model.productInterface import ProductInterface
from utils.readFile import ReadFile


class ProductImplApi(ProductInterface):
	'''
		商品搜索
	'''
	def productSearch(self):
		httpRequest = HttpRequest()
		httpRequest.host = (ReadFile.readEnvConfigYaml((os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+"\\config\\config.yml"))['environment'])
		httpRequest.path = '/wx/goods/list'
		httpRequest.method = 'get'
		httpRequest.header = {"X-Litemall-Token":self.token}
		httpRequest.type = 'form'
		httpRequest.urlParams = {"keyword": self.product.keyword, "page": self.product.page,"limit":self.product.limit}
		response = httpRequest.send()
		return response

	'''
		查询商品详情
	'''
	def productDetail(self):
		httpRequest = HttpRequest()
		httpRequest.host = (ReadFile.readEnvConfigYaml((os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\config\\config.yml"))['environment'])
		httpRequest.path = '/wx/goods/detail'
		httpRequest.method = 'get'
		httpRequest.header = {"X-Litemall-Token":self.token}
		httpRequest.type = 'form'
		httpRequest.urlParams = {"id": self.product.id}
		response = httpRequest.send()
		return response

	'''
		添加商品到购物车
	'''
	def addCart(self):
		httpRequest = HttpRequest()
		httpRequest.host = (ReadFile.readEnvConfigYaml(
			(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\config\\config.yml"))['environment'])
		httpRequest.path = '/wx/cart/add'
		httpRequest.method = 'post'
		httpRequest.header = {"X-Litemall-Token": self.token}
		httpRequest.type = 'json'
		httpRequest.bodyData = {"goodsId": self.product.id,"number":self.product.number,"productId":self.product.productId}
		response = httpRequest.send()
		return response

	'''
		获取购物车列表
	'''
	def cartList(self):
		httpRequest = HttpRequest()
		httpRequest.host = (ReadFile.readEnvConfigYaml(
			(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "\\config\\config.yml"))['environment'])
		httpRequest.path = '/wx/cart/index'
		httpRequest.method = 'get'
		httpRequest.header = {"X-Litemall-Token": self.token}
		response = httpRequest.send()
		return response